package fr.waitandsee.fragments;

import fr.waitandsee.FragmentController;
import fr.waitandsee.R;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SettingsFragment extends Fragment implements PageFragmentModel {
	
	private static final String TAG = "SettingsFragment";
	
	private TextView tvUsername;
	private TextView tvEmail;
	private TextView tvConditions;
	private TextView tvClearFields;
	private LinearLayout llContact;
	private TextView tvDisconnection;
	private TextView tvBack;
	
	private FragmentController fragmentController;
	private Handler handler;
	
	public static SettingsFragment newInstance(/*parameters*/) {
		SettingsFragment fragment = new SettingsFragment();
		// set parameters if needed : 
	    // Bundle bundle = new Bundle(1);
	    // bundle.putInt(EXTRA_NAME, param);
	    // fragment.setArguments(bundle);
	    return fragment ;
	}
	
	
	  /////////////////////////
	 // FRAGMENT LIFE CYCLE //
	/////////////////////////
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate.savedInstanceState="+savedInstanceState);
		this.fragmentController = (FragmentController) getActivity();
		this.handler = new Handler();
	}
	
 	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 		Log.d(TAG, "onCreateView.savedInstanceState="+savedInstanceState);
		if (savedInstanceState != null) {
			// get saved parameters if needed : 
			// param = savedInstanceState.getInt(EXTRA_NAME);
		} else {
			if(getArguments() != null) {
				// get parameters if needed :
				// param = getArguments().getInt(EXTRA_NAME);
			}
		}
		View v = inflater.inflate(R.layout.fragment_settings, container, false);
		return v;
	}
	
 	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
 		Log.d(TAG, "onViewCreated.savedInstanceState="+savedInstanceState);
		fetchViews();
		customizeViews();
		setListeners();
	}
 	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		Log.d(TAG, "onSaveInstanceState.outState="+outState);
		super.onSaveInstanceState(outState);
		// save parameters if needed :
		// outState.putInt(EXTRA_NAME, param);
	}
 	
	@Override
 	public void fetchViews() {
		this.tvUsername = (TextView) getView().findViewById(R.id.textView_username);
		this.tvEmail = (TextView) getView().findViewById(R.id.textView_email);
		this.tvConditions = (TextView) getView().findViewById(R.id.textView_conditions);
		this.tvClearFields = (TextView) getView().findViewById(R.id.textView_clear_feed);
		this.llContact = (LinearLayout) getView().findViewById(R.id.linearLayout_contact);
		this.tvDisconnection = (TextView) getView().findViewById(R.id.textView_disconnection);
		this.tvBack = (TextView) getView().findViewById(R.id.textView_back);
 	}
	
	@Override
	public void customizeViews() {
		//TODO set username
		this.tvUsername.setText("Anthony"); 		//DEBUG
		//TODO set email
		this.tvEmail.setText("anthony@gmail.com"); 	//DEBUG
	}
 	
	@Override
	public void setListeners() {
		this.tvConditions.setOnTouchListener(onConditionsTouchListener);
		this.tvClearFields.setOnTouchListener(onClearFeedTouchListener);
		this.llContact.setOnTouchListener(onContactTouchListener);
		this.tvDisconnection.setOnTouchListener(onDisconnectionTouchListener);
		this.tvBack.setOnTouchListener(onBackTouchListener);
 	}
	
	
	  ///////////////
	 // LISTENERS //
	///////////////
	
	private OnTouchListener onConditionsTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.i(TAG, "onConditionsTouchListener");
			tvConditions.setOnTouchListener(null); //stand-by listener to prevent multi opening
			new AlertDialog.Builder(getActivity(), AlertDialog.THEME_HOLO_LIGHT)
			.setTitle(R.string.conditions)
			.setMessage(Html.fromHtml(getString(R.string.conditions_content)))
			.show();
			//TODO action on "conditions" textview touched 
			handler.postDelayed(delayAfterTouchCallback, 200);
			return false;
		}
 	};
 	
	private OnTouchListener onClearFeedTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.i(TAG, "onClearFeedTouchListener");
			tvClearFields.setOnTouchListener(null); //stand-by listener to prevent multi opening
			//TODO action on "clear feed" textview touched 
			handler.postDelayed(delayAfterTouchCallback, 200);
			return false;
		}
 	};
 	
	private OnTouchListener onContactTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.i(TAG, "onContactTouchListener");
			llContact.setOnTouchListener(null); //stand-by listener to prevent multi opening
			Intent emailIntent = new Intent(Intent.ACTION_SENDTO, 
					Uri.fromParts("mailto", getActivity().getString(R.string.email_waitandsee), null));
			emailIntent.putExtra(Intent.EXTRA_SUBJECT, getActivity().getString(R.string.email_subject));
			startActivity(Intent.createChooser(emailIntent, "Send email..."));
			handler.postDelayed(delayAfterTouchCallback, 200);
			return false;
		}
 	};
 	
	private OnTouchListener onDisconnectionTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.i(TAG, "onDisconnectionTouchListener");
			tvDisconnection.setOnTouchListener(null); //stand-by listener to prevent multi opening
			//TODO action on "disconnection" textview touched 
			handler.postDelayed(delayAfterTouchCallback, 200);
			return false;
		}
 	};
	
	private OnTouchListener onBackTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.i(TAG, "onBackTouchListener");
			tvBack.setOnTouchListener(null); //stand-by listener to prevent multi opening
			fragmentController.switchContent(ContactPickerFragment.newInstance());
			return false;
		}
 	};
 	
 	private Runnable delayAfterTouchCallback = new Runnable() {
 		@Override 
 		public void run() {
 			tvConditions.setOnTouchListener(onConditionsTouchListener);
 			tvClearFields.setOnTouchListener(onClearFeedTouchListener);
 			tvDisconnection.setOnTouchListener(onDisconnectionTouchListener);
 			llContact.setOnTouchListener(onContactTouchListener);
 		}
 	};
 	
}
